package com.cjd.common.utils

import android.content.Context
import android.os.Build
import android.os.Looper
import android.text.InputType
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import java.lang.reflect.Method
import java.nio.charset.Charset
import java.util.*

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2016/1/31.
 * description
 */
object CommonUtil {
    /**
     * 弹出键盘
     *
     * @param editText
     */
    fun showSoftInput(editText: EditText) {
        val timer = Timer()
        timer.schedule(object : TimerTask() {
            override fun run() {
                val inputManager = editText
                    .context.getSystemService(
                    Context.INPUT_METHOD_SERVICE
                ) as InputMethodManager
                inputManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
            }
        }, 200)
    }

    /**
     * 隐藏键盘
     */
    fun hideSoftInput(editText: EditText) {
        val imm = editText
            .context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        //		imm.showSoftInput(editText,InputMethodManager.SHOW_FORCED);
        imm.hideSoftInputFromWindow(editText.windowToken, 0)
    }

    /**
     * 禁止Edittext弹出软件盘，光标依然正常显示。
     */
    fun disableShowSoftInput(editText: EditText) {
        if (Build.VERSION.SDK_INT <= 10) {
            editText.inputType = InputType.TYPE_NULL
        } else {
            val cls = EditText::class.java
            var method: Method
            try {
                method = cls.getMethod("setShowSoftInputOnFocus", Boolean::class.javaPrimitiveType)
                method.isAccessible = true
                method.invoke(editText, false)
            } catch (e: Exception) {
                // TODO: handle exception
            }

            try {
                method = cls.getMethod("setSoftInputShownOnFocus", Boolean::class.javaPrimitiveType)
                method.isAccessible = true
                method.invoke(editText, false)
            } catch (e: Exception) {
                // TODO: handle exception
            }

        }
    }

    fun getAlpha(scrollY: Int, maxPoint: Int): Double {
        val max = 255
        if (scrollY * 0.5 > max)
            return maxPoint.toDouble()
        else if (scrollY * 0.5 < 10)
            return 0.0
        else {
            var alpha = 0.0
            alpha = (maxPoint / max).toDouble() * scrollY.toDouble() * 0.5
            return alpha
        }
    }

    fun isMainThread(): Boolean {
        return Looper.getMainLooper().thread.id == Thread.currentThread().id
    }

    /**
     * 从assets 文件夹中获取文件并读取数据
     */
    fun getFromAssets(context: Context, fileName: String): String {
        var result = ""
        try {
            val inputStream = context.resources.assets.open(fileName)
            //获取文件的字节数
            val lenght = inputStream.available()
            //创建byte数组
            val buffer = ByteArray(lenght)
            //将文件中的数据读到byte数组中
            inputStream.read(buffer)
            result = String(buffer, Charset.forName("UTF-8"))
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return result
    }

    @JvmStatic
    fun isExistSDCard(): Boolean {
        return android.os.Environment.getExternalStorageState() == android.os.Environment.MEDIA_MOUNTED
    }
}